package com.mywork.util;

import org.springframework.util.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtil {

    public static boolean isEmpty(String value) {

        return value == null || value.isEmpty();
    }

    public static boolean isEmptyValue(String value) {

        return isEmpty(value) || "null".equals(value);
    }

    public static String hideTelInfo(String phone) {
        if (StringUtils.isEmpty(phone)) {
            return "";
        }
        if (phone.length() > 7) {
            return phone.substring(0, 3) + "*****" + phone.substring(phone.length() - 3, phone.length());
        } else {
            return phone;
        }

    }

    public static String hideUserNameInfo(String userName) {
        //少于等于2个字符串的username不做特殊处理
        if (StringUtils.isEmpty(userName)) {
            return userName;
        }
        if (userName.length() == 2) {
            return MessageFormat.format("{0}*", userName.substring(0, 1));
        }

        if (userName.length() <= 5) {
            if (userName.length() == 3) {
                return MessageFormat.format("{0}*{1}", userName.substring(0, 1), userName.substring(userName.length() - 1));
            } else if (userName.length() == 4) {
                return MessageFormat.format("{0}**{1}", userName.substring(0, 1), userName.substring(userName.length() - 1));
            } else {
                return MessageFormat.format("{0}***{1}", userName.substring(0, 1), userName.substring(userName.length() - 1));
            }
        } else {
            int strLength = userName.length();
            int StartIndex = strLength / 2 - 2;
            return MessageFormat.format("{0}****{1}", userName.substring(0, StartIndex), userName.substring(StartIndex + 4));
        }
    }


    public static List<Integer> StringToListInt(String[] arrs) {
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i < arrs.length; i++) {
            if (!arrs[i].equals("null") || arrs[i].equals("")) {
                list.add(Integer.valueOf(arrs[i]));
            }
        }
        return list;
    }

    public static List<Integer> StringToListInt(String items) {
        String[] arrs = items.split(",");
        return StringToListInt(arrs);
    }

    public static boolean hasEmpty(String... values) {
        for (String item : values) {
            if (isEmpty(item)) {
                return true;
            }
        }
        return false;
    }

    public static boolean hasNull(Object... values) {
        for (Object item : values) {
            if (item == null) {
                return true;
            }
        }
        return false;
    }

    public static boolean allNull(Object... values) {
        for (Object item : values) {
            if (item != null) {
                return false;
            }
        }
        return true;
    }

    public static boolean allEmpty(String... values) {
        for (String item : values) {
            if (!isEmpty(item)) {
                return false;
            }
        }
        return true;
    }

    public static Object objectTrim(Object obj) {
        if (obj != null) {
            if (obj instanceof String) {
                obj = ((String) obj).trim();
            }
        }
        return obj;
    }

    public static String getString(Object obj) {
        if (obj == null) {
            return "";
        }
        return obj.toString();
    }

    public static String getExceptionInfos(Exception ex) {
        StringWriter sw = new StringWriter();
        ex.printStackTrace(new PrintWriter(sw));
        return sw.toString();
    }

    public static String delHTMLTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
        String regEx_special = "\\&[a-zA-Z]{1,10};";

        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); //过滤

        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); //过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); //过滤html标签

        Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
        Matcher m_special = p_special.matcher(htmlStr);
        htmlStr = m_special.replaceAll(""); //过滤特殊标签

        return htmlStr.trim(); //返回文本字符串 
    }

    public static String delSpace(String htmlStr) {
        htmlStr = htmlStr.replaceAll("\n", "");
        htmlStr = htmlStr.replaceAll("\t", "");
        return htmlStr.trim(); //返回文本字符串 
    }

    public static boolean isPhone(String phone) {
        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        if (phone.length() != 11) {
            return false;
        } else {
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(phone);
            return m.matches();
        }
    }

    public static String implode(String glue, String... strings) {
        if (glue == null) {
            glue = ",";
        }
        StringBuilder sb = new StringBuilder();
        for (String string : strings) {
            sb.append(string).append(glue);
        }
        String string = sb.toString();
        return string.substring(0, string.length() - 1);

    }

    public static String implode(String glue, Object... objs) {
        if (glue == null) {
            glue = ",";
        }
        StringBuilder sb = new StringBuilder();
        for (Object obj : objs) {
            sb.append(obj).append(glue);
        }
        String string = sb.toString();
        return string.substring(0, string.length() - 1);

    }

    public static String implode(String glue, List<String> strList) {
        if (glue == null) {
            glue = ",";
        }
        if (strList.isEmpty()) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String string : strList) {
            sb.append(string).append(glue);
        }
        String string = sb.toString();
        return string.substring(0, string.length() - 1);
    }

    public static String implode(List<String> strList) {
        return implode(",", strList);
    }

    public static Date strToDate(String timeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date time = null;
        if (!StringUtil.isEmpty(timeStr)) {
            try {
                time = sdf.parse(timeStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return time;
    }


    public static String getOrderId() {


   /*     Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYMMdd");
        String format = simpleDateFormat.format(date);*/

        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if (hashCodeV < 0) {//有可能是负数
            hashCodeV = -hashCodeV;
        }
        // 0 代表前面补充0
        // 8 代表长度为8
        // d 代表参数为正数型
        return machineId + String.format("%015d", hashCodeV);
        //return format + hashCodeV;
    }

    public static Map<String, String> paramToMap(String paramStr) {
        String[] params = paramStr.split("&");
        Map<String, String> resMap = new HashMap<String, String>();
        for (String param1 : params) {
            String[] param = param1.split("=");
            if (param.length >= 2) {
                String key = param[0];
                StringBuilder value = new StringBuilder(param[1]);
                for (int j = 2; j < param.length; j++) {
                    value.append("=").append(param[j]);
                }
                resMap.put(key, value.toString());
            }
        }
        return resMap;
    }

    /**
     * 判断是否数字
     *
     * @param strValue
     * @return
     */
    public static boolean isNumber(String strValue) {

        boolean result = strValue.matches("[0-9]+");
        return result;
    }

    /**
     * 判断是否
     *
     * @param strValue
     * @return
     */
    public static boolean isCard(String strValue) {
        return strValue.matches("^[a-zA-Z0-9]+$");
    }


    public static String splicAddress(Integer build, Integer unit, String address) {
        String addressStr = "";
        if (null != build && build != 0) {
            addressStr += build;
            addressStr += "-";
        }
        if (null != unit && unit != 0) {
            addressStr += unit;
            addressStr += "-";
        }
        if (!StringUtil.isEmpty(address)) {
            addressStr += address;
        }
        return addressStr;
    }

    /**
     * 判断是否是中文
     *
     * @param countname
     * @return
     */
    public static boolean checkcountname(String countname) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(countname);
        if (m.find()) {
            return true;
        }
        return false;
    }
}
